package com.transfer.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.staff.entity.Staff;
import com.transfer.entity.PersonTransfer;

public class PersonTransferQueryDTO {
	private Long id;
	private String originalDepa;//原部门
	private String originalPosi;//原职位
	private String newDepa;//调入部门
	private String newPosi;//调入职位
	private Date deployDate;//调动日期
	private String staffName;
	private String staffNum;
	
	private Long staffId;

	public PersonTransferQueryDTO(PersonTransfer pt, Staff staff) {
		super();
		this.id = pt.getId();
		this.originalDepa = pt.getOriginalDepa();
		this.originalPosi = pt.getOriginalPosi();
		this.newDepa = pt.getNewDepa();
		this.newPosi = pt.getNewPosi();
		this.deployDate = pt.getDeployDate();
		this.staffName = pt.getStaffName();
		this.staffNum = pt.getStaffNum();
		this.staffId = staff.getId();
	}
	public PersonTransferQueryDTO() {
		super();
	}
	public Long getId() {
		return id;
	}
	public String getOriginalDepa() {
		return originalDepa;
	}
	public String getOriginalPosi() {
		return originalPosi;
	}
	public String getNewDepa() {
		return newDepa;
	}
	public String getNewPosi() {
		return newPosi;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getDeployDate() {
		return deployDate;
	}
	public String getStaffName() {
		return staffName;
	}
	public String getStaffNum() {
		return staffNum;
	}
	public Long getStaffId() {
		return staffId;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setOriginalDepa(String originalDepa) {
		this.originalDepa = originalDepa;
	}
	public void setOriginalPosi(String originalPosi) {
		this.originalPosi = originalPosi;
	}
	public void setNewDepa(String newDepa) {
		this.newDepa = newDepa;
	}
	public void setNewPosi(String newPosi) {
		this.newPosi = newPosi;
	}
	public void setDeployDate(Date deployDate) {
		this.deployDate = deployDate;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}
	public void setStaffNum(String staffNum) {
		this.staffNum = staffNum;
	}
	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}
	
	//提供static的工具方法：根据当前 userQueryDTO 对象来组装动态查询条件
	public static Specification<PersonTransfer> getSpecification(PersonTransferQueryDTO userQueryDTO)
	{
		Specification<PersonTransfer> spec = new Specification<PersonTransfer>() {
			public Predicate toPredicate(Root<PersonTransfer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getStaffName())) {
					 Predicate  p1 =  cb.like(root.get("staffName").as(String.class),"%"+ userQueryDTO.getStaffName() + "%");
					 list.add(p1);
				 }
				 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getStaffNum())) {
					 Predicate  p2 =  cb.like(root.get("staffNum").as(String.class),"%"+ userQueryDTO.getStaffNum() + "%");
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
